package com.controller;

import com.bean.*;
import com.dao.admission_batchMapper;
import com.dao.high_school_infoMapper;
import com.dao.high_school_major_infoMapper;
import com.dao.voluntary_infoMapper;
import com.model.ResultModel;
import com.model.SubmitModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
@SessionAttributes("resultlist")
public class SchoolController {
    @Autowired
    private voluntary_infoMapper voluntaryInfoMapper;
    @Autowired
    private high_school_infoMapper highSchoolInfoMapper;
    @Autowired
    private high_school_major_infoMapper highSchoolMajorInfoMapper;
    @Autowired
    private admission_batchMapper admissionBatchMapper;

    @RequestMapping("/school_search")
    public String School_info_search(){
        return "school_info";
    }

    @RequestMapping("/check_vol")
    @ResponseBody
    public String check_vol(int stu_id){
//        System.out.println("获取到的"+stu_id);
        voluntary_infoExample example1 = new voluntary_infoExample();
        voluntary_infoExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andSTU_IDEqualTo(stu_id);
        List<voluntary_info> voluntary_infos = voluntaryInfoMapper.selectByExample(example1);
        if(voluntary_infos.isEmpty()){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/search_result")
    @ResponseBody
    public String search_info(SubmitModel model, ModelMap map){
        //ArrayList<ResultModel> resultList = new ArrayList<>();
        String str = "";
        int batch_id = model.getBatch_id();
        int sch_id = model.getSch_id();
        int major_id= model.getMajor_id();
//        System.out.println(batch_id);
        admission_batchExample batchExample = new admission_batchExample();
        admission_batchExample.Criteria criteria = batchExample.createCriteria();
        if(batch_id != 0){
            criteria.andBATCH_IDEqualTo(batch_id);
        }
        List<admission_batch> admissionBatches = admissionBatchMapper.selectByExample(batchExample);
        for(int k =0;k<admissionBatches.size();k++) {
            high_school_infoExample example = new high_school_infoExample();
            high_school_infoExample.Criteria criteria1 = example.createCriteria();
            criteria1.andSCH_BATCHEqualTo(admissionBatches.get(k).getBATCH_ID());
            if (sch_id != 0) {
                criteria1.andSCH_IDEqualTo(sch_id);
            }

            List<high_school_info> list = highSchoolInfoMapper.selectByExample(example);
            for (int i = 0; i < list.size(); i++) {
                high_school_major_infoExample majorInfoExample = new high_school_major_infoExample();
                high_school_major_infoExample.Criteria criteria2 = majorInfoExample.createCriteria();
                criteria2.andSCH_IDEqualTo(list.get(i).getSCH_ID());
                if (major_id != 0) {
                    criteria2.andMAJOR_IDEqualTo(major_id);
                }
                List<high_school_major_info> majorInfos = highSchoolMajorInfoMapper.selectByExample(majorInfoExample);
                for (int j = 0; j < majorInfos.size(); j++) {
                    str += list.get(i).getSCH_ID()+"^"+ list.get(i).getSCH_NAME()+"^"+ list.get(i).getSCH_LOCATION()+"^"+
                    admissionBatches.get(0).getBATCH_NAME()+"^"+ list.get(i).getSCH_LINE()+"^"+ majorInfos.get(j).getMAJOR_NAME()+
                    "^"+majorInfos.get(j).getLAST_SCORE_LINE()+"|";
                }
            }
        }

        return str;
    }

    @RequestMapping("/find_by_batch")
    @ResponseBody
    public String findSchools(int batch_id){
        high_school_infoExample example = new high_school_infoExample();
        high_school_infoExample.Criteria criteria = example.createCriteria();
        criteria.andSCH_BATCHEqualTo(batch_id);
        List<high_school_info> list = highSchoolInfoMapper.selectByExample(example);
        String str = "";
        if(!list.isEmpty()){
            for(int i=0;i<list.size();i++){
                str += list.get(i).getSCH_ID()+"^"+list.get(i).getSCH_NAME()+"|";
            }
        }
        return str;
    }

    @RequestMapping("/major_search")
    @ResponseBody
    public String findMajors(int sch_id){
        high_school_major_infoExample example = new high_school_major_infoExample();
        high_school_major_infoExample.Criteria criteria = example.createCriteria();
        criteria.andSCH_IDEqualTo(sch_id);
        List<high_school_major_info> list = highSchoolMajorInfoMapper.selectByExample(example);
        String str="";
        if(!list.isEmpty()){
            for(int i=0;i<list.size();i++){
                str += list.get(i).getMAJOR_ID()+"^"+list.get(i).getMAJOR_NAME()+"|";
            }
        }
        return str;
    }
}